package com.imooc.bilibili.api.support;

import com.imooc.bilibili.domain.exception.ConditionException;
import com.imooc.bilibili.service.util.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author GJH
 * @version V1.0
 * @Package com.imooc.bilibili.api.support
 * @date 2022/3/29 10:52
 * @Copyright © XXXX-XXXX  搬砖工程师
 */
@Component
public class UserSupport {
    public Long getCurentUserId() {
        //抓取请求上下文的
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = requestAttributes.getRequest().getHeader("token");  //获取令牌
        //获取userId
        Long userId = TokenUtil.verifyToken(token);
        if(userId<0){
            throw new ConditionException("非法用户！");
        }
        return userId;

    }

    public String getCurentUserPhone() {
        //抓取请求上下文的
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = requestAttributes.getRequest().getHeader("token");  //获取令牌
        //获取userPhone
        Long userPhone = TokenUtil.verifyToken(token);
        if(userPhone==null){
            throw new ConditionException("非法用户！");
        }
        return "用户手机号可用";

    }



}

